package com.jiang.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.annotation.WebServlet;

import org.apache.commons.lang.StringUtils;

import com.jiang.factory.ServiceFactory;
import com.jiang.service.INewsService;
import com.jiang.service.impl.NewsServiceImpl;
import com.jiang.vo.News;
import com.jiang.vo.ResultSet;

@WebServlet(name = "newsAction", urlPatterns = "/newsAction/*")
public class NewsAction extends BaseAction {

	private static final long serialVersionUID = -4417238962770449042L;

	/**
	 * 查询所有的新闻纪录
	 * 
	 * @return
	 */
	public ResultSet findAllNews() {
		ResultSet rs = new ResultSet();
		INewsService newsService = ServiceFactory.getInstance(NewsServiceImpl.class);
		try {
			List<News> newsList = newsService.findAll();
			rs.setSuccess(true);
			rs.setData(newsList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 查询单条纪录的详情
	 * 
	 * @return
	 */
	public ResultSet findById() {
		ResultSet rs = new ResultSet();
		String nid = request.getParameter("nid");
		// 本来用框架拦截器进行数据验证，现在简单之余进行简单判断
		if (StringUtils.isNotBlank(nid)) {
			INewsService newsService = ServiceFactory.getInstance(NewsServiceImpl.class);
			try {
				News news = newsService.findById(Integer.parseInt(nid));
				rs.setSuccess(true);
				rs.setData(news);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return rs;
	}

	public ResultSet addNews() {

		ResultSet rs = new ResultSet();
		String title = request.getParameter("title");
		String pubdate = request.getParameter("pubdate");
		String content = request.getParameter("content");
		try {
			if (StringUtils.isNotBlank(title) && StringUtils.isNotBlank(pubdate) && StringUtils.isNotBlank(content)) {
				News news = new News();
				news.setTitle(title);
				news.setContent(content);
				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
				Date date = sf.parse(pubdate);
				news.setPubdate(date);
				INewsService newsService = ServiceFactory.getInstance(NewsServiceImpl.class);
				rs.setSuccess(newsService.addNews(news));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rs;
	}

	/**
	 * 分页查询
	 * 
	 * @return
	 */
	public ResultSet findSplit() {
		ResultSet rs = new ResultSet();
		try {
			String curPage = request.getParameter("cp");
			String lineSize = request.getParameter("ls");
			if (StringUtils.isNotBlank(curPage) && StringUtils.isNotBlank(lineSize)) {
				INewsService newsService = ServiceFactory.getInstance(NewsServiceImpl.class);
				// 暂时没有搜索分页查询，所以先写死
				Map<String, Object> resultMap = newsService.findSplit("nid", "", Integer.parseInt(curPage),
						Integer.parseInt(lineSize));
				rs.setSuccess(true);
				rs.setData(resultMap);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rs;
	}

	/**
	 * 批量查询
	 * 
	 * @return
	 */
	public ResultSet batchDel() {
		ResultSet rs = new ResultSet();
		try {
			String nids = request.getParameter("nids");
			if (StringUtils.isNotBlank(nids)) {
				String[] allnids = nids.split(",");
				Set<Integer> nid_set = new HashSet<Integer>();
				for (String nid : allnids) {
					nid_set.add(Integer.parseInt(nid));
				}
				INewsService newsService = ServiceFactory.getInstance(NewsServiceImpl.class);
				rs.setSuccess(newsService.batchRemove(nid_set));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 修改纪录
	 * 
	 * @return
	 */
	public ResultSet update() {

		ResultSet rs = new ResultSet();
		String nid = request.getParameter("nid");
		String title = request.getParameter("title");
		String pubdate = request.getParameter("pubdate");
		String content = request.getParameter("content");
		try {
			if (StringUtils.isNotBlank(title) && StringUtils.isNotBlank(pubdate) && StringUtils.isNotBlank(content)) {
				News news = new News();
				news.setNid(Integer.parseInt(nid));
				news.setTitle(title);
				news.setContent(content);
				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
				Date date = sf.parse(pubdate);
				news.setPubdate(date);
				INewsService newsService = ServiceFactory.getInstance(NewsServiceImpl.class);
				rs.setSuccess(newsService.updateNews(news));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}

}
